package com.example.teststarter.controller;

import cn.top.common.FieldTypeConstant;
import cn.top.common.SearchFieldTypeConstant;
import cn.top.pojo.Doc;
import cn.top.pojo.DocAttr;
import cn.top.pojo.QueryParamDto;
import cn.top.pojo.ResultModel;
import cn.top.service.TopLuceneIndexService;
import cn.top.service.TopLuceneSearchService;
import com.example.starter.service.DemoService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author zhangjiawen
 * @Date: 2022/2/23 15:21
 */
@RestController
public class TestController {
    @Resource
    DemoService demoService;
    @Resource
    TopLuceneIndexService topLuceneIndexService;
    @Resource
    TopLuceneSearchService searchService;

    /**
     * 检索
     * @param name
     * @param page
     * @return
     * @throws Exception
     */
    @GetMapping("/find")
    public ResultModel find(String name,Integer page) throws Exception {
        List<QueryParamDto> list=new ArrayList<>();
        QueryParamDto queryParamDto=new QueryParamDto();
        queryParamDto.setParam(name);
        queryParamDto.setParamType(SearchFieldTypeConstant.STRING_FIELD);
        queryParamDto.setTargetFields(Arrays.asList("content"));
        list.add(queryParamDto);
        ResultModel query = searchService.query(list, page);

        return query;

    }

    /**
     * 创建索引
     * @return
     * @throws IOException
     */
    @GetMapping("/add")
    public String add() throws IOException {
        List<Doc> param= new ArrayList<>();
        Doc doc=new Doc();
        doc.setId("1");

        List<DocAttr> attrList=new ArrayList<>();
        //添加属性1
        DocAttr docAttr=new DocAttr();
        docAttr.setKey("content");
        docAttr.setFieldType(FieldTypeConstant.TEXT_FIELD_Y);
        docAttr.setValue("奥特曼可否于怪兽一战？");
        attrList.add(docAttr);
        //添加属性2……

        doc.setDocAttr(attrList);
        param.add(doc);
         topLuceneIndexService.createDocIndex(param);
         return "success";
    }

    /**
     * 更新索引
     * @return
     * @throws IOException
     */
    @GetMapping("/update")
    public String update() throws IOException {

        Doc doc=new Doc();
        doc.setId("1");

        List<DocAttr> attrList=new ArrayList<>();
        //添加属性1
        DocAttr docAttr=new DocAttr();
        docAttr.setKey("content");
        docAttr.setFieldType(FieldTypeConstant.TEXT_FIELD_Y);
        docAttr.setValue("奥特曼可否于怪兽一战？升级版");
        attrList.add(docAttr);
        //添加属性2……
        doc.setDocAttr(attrList);
        topLuceneIndexService.updateDocIndex(doc);
        return "success";

    }

    /**
     * 删除索引
     * @return
     * @throws IOException
     */
    @GetMapping("/del")
    public String del() throws IOException {
        Doc doc=new Doc();
        doc.setId("1");
        topLuceneIndexService.delDocIndex(doc);
        return "success";

    }

    /**
     * 清库索引
     * @return
     * @throws Exception
     */
    @GetMapping("/delAll")
    public String delAll() throws Exception {

        topLuceneIndexService.delAllDocIndex();
        return "success";

    }

    /**
     * 清空并创建索引
     * @return
     * @throws Exception
     */
    @GetMapping("/reload")
    public String reload() throws Exception {
        List<Doc> param= new ArrayList<>();
        Doc doc=new Doc();
        doc.setId("1");

        List<DocAttr> attrList=new ArrayList<>();
        //添加属性1
        DocAttr docAttr=new DocAttr();
        docAttr.setKey("content");
        docAttr.setFieldType(FieldTypeConstant.TEXT_FIELD_Y);
        docAttr.setValue("奥特曼可否于怪兽一战？");
        attrList.add(docAttr);
        //添加属性2……

        doc.setDocAttr(attrList);
        param.add(doc);
        topLuceneIndexService.reloadAllDocIndex(param);
        return "success";

    }

}
